<?php

namespace App\Http\Controllers\Index;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Session;
use App\Http\Service\User\UserService;


class IndexController extends Controller
{


    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 首页
     */
    public function home()
    {
        return view("Home.home");
    }


    /**
     * @param Request $request
     * 获取用户账号信息
     */
    public function getAccountInfo(Request $request)
    {
        $where = [];
        $where['token'] = Session::get('MEMBER_TOKEN');

        $user = new UserService();
        $user->getAccountList($where);
        return view('Index.index');

    }

    /**
     * @param Request $request
     * 获取用户账号列表
     */
    public function getAccountList(Request $request)
    {
        $where = [];
        $where= $request->except('_token');
        $where['token'] = Session::get('MEMBER_TOKEN');
        $user = new UserService();
        $data = $user->getAccountList($where);

        if($data['error_code'] != 0)
        {
            $message =  $data['error_message'];
            echo "<script>confirm('{$message}')?location.href='/' : history.back() ;</script>";
            exit();
        }

        if($data['error_code'] == '0')
        {
            $data = $data['data'];
        }

        return view('Account.account')->with('data',$data);

    }


    // 获取某一用户在线列表
    public function getBillList(Request $request)
    {
        $where = [];
        $where= $request->except('_token');
        $where['token'] = Session::get('MEMBER_TOKEN');

        $user = new UserService();
        $data = $user->getBillList($where);
        if($data['error_code'] != 0)
        {
            $message =  $data['error_message'];
            echo "<script>confirm('{$message}')?location.href='/' : history.back() ;</script>";
            exit();
        }

        $data = $data['data'];
//        dd($data);
        return view('Bill.list')->with('data',$data);
    }


    public function getAccountOnline(Request $request)
    {
        $where = [];
        $where= $request->except('_token');
        $where['token'] = Session::get('MEMBER_TOKEN');
    }

    /**
     * @param Request $request
     * 添加账号
     */
    public function addAccount(Request $request)
    {
        $where = [];
        $where= $request->except('_token');
        $where['token']=Session::get('MEMBER_TOKEN');

        $where['price'] = $where['concent_num'] * $where['date'] *30;


        $where['order_id'] = self::createUuid();

        $user = new UserService();
        $data = $user->addAccount($where);

        if($data['error_code'] == '0')
        {
            echo '<script>location.href="/account";alert("账号添加成功！");</script>';
        }else{
            echo "<script>confirm('{$data['error_message']}')?location.href='/account' : history.back() ;</script>";
            exit();
        }

    }




    //可以指定前缀
    static function createUuid($prefix = "")
    {
        $str = md5(uniqid(mt_rand(), true));
        $uuid  = substr($str,0,8) . '-';
        $uuid .= substr($str,8,4) . '-';
        $uuid .= substr($str,12,4) . '-';
        $uuid .= substr($str,16,4) . '-';
        $uuid .= substr($str,20,12);
        return $prefix . $uuid;
    }



    /**
     * @param Request $request
     * @return $this
     * 获取账号 在线信息
     */
    public function getAccountOnlineInfo(Request $request)
    {
        $where = [];
        $where['account_id'] = $request->input('account_id');
        $user = new UserService();
        $data = $user->getAccountOnlineInfo($where);
        if($data['error_code'] == '0')
        {
            $data = $data['data'];
        }else{
            echo "<script>confirm('{$data['error_message']}')?location.href='/account' : history.back() ;</script>";
            exit();
        }

        $data['account_id'] = $where['account_id'];
        return view('Account.online')->with('data',$data);

    }

    /**
     * @param Request $request
     * 禁用用户
     */
    public function forbiddenAccount(Request $request)
    {
        $where  = [];
        $where = $request->all();

        $account_id = $where['account_id'];
        unset($where['account_id']);
        $user = new UserService();
        $data = $user->forbiddenAccount($where);
        if($data['data'] == 'false')
        {
            echo "<script>location.href='/getaccountonlieInfo?account_id={$account_id}';alert('线路禁用失败！');</script>";
        }else{
            echo "<script>location.href='/getaccountonlieInfo?account_id={$account_id}';alert('线路禁用成功！');</script>";
        }

    }

    /**
     * @param Request $request
     * 修改账户信息
     */
    public function updateAccount(Request $request)
    {
        $where = [];
        $where= $request->except('_token');

        $user = new UserService();
        $data = $user->updateAccount($where);

        if($data['error_code'] == '0')
        {
            echo '<script>location.href="/account";alert("账号信息修改成功！");</script>';
        }else{
            echo "<script>confirm('{$data['error_message']}')?location.href='/account' : history.back() ;</script>";
            exit();
        }
    }
    
    












}
